import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:ball_lion/utils/date_time_utils.dart';
import 'package:ball_lion/entity/record/football/zq_schedule_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';

class ScheduleItem extends StatelessWidget {
  const ScheduleItem({super.key, required this.data});

  final ScheduleEntity? data;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
            height: 36,
            width: Get.width,
            padding: const EdgeInsets.only(left: 16),
            alignment: Alignment.centerLeft,
            decoration: const BoxDecoration(color: Colours.greyF2F3F7),
            child: QsText(
              data!.roundName!,
              fontSize: 13,
              color: Colours.N9,
              fontWeight: FontWeight.w500,
            )),
        ListView.separated(
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          itemCount: data?.schedules?.length ?? 0,
          separatorBuilder: (context, index) => const Divider(
              height: 0.5, color: Colours.N3, indent: 16, endIndent: 16),
          itemBuilder: (context, index) => ScheduleCell(
            index: index,
            schedules: data!.schedules![index],
            recordType: data?.recordType,
          ),
        ),
      ],
    );
  }
}

class ScheduleCell extends StatelessWidget {
  const ScheduleCell(
      {super.key,
      required this.schedules,
      required this.index,
      this.padding = 16,
      this.needColor = true,
      this.recordType});

  final int index;
  final double padding;
  final bool needColor;
  final int? recordType;
  final Schedules? schedules;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      // onTap: () {
      //   if (schedules?.matchId != null) {
      //     Utils.onEvent("data_match_view",
      //         params: {"match_id": schedules?.matchId});
      //     Get.toNamed(
      //         recordType == 0 ? Routes.zqMatchDetail : Routes.lqMatchDetail,
      //         arguments: schedules?.matchId);
      //   }
      // },
      child: Container(
        height: 68,
        padding: EdgeInsets.symmetric(horizontal: padding),
        color: Colours
            .N1, //index == 0 || index % 2 == 0 ? Colours.N3 : Colours.B1,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            QsText(
                (schedules?.matchTime?.length ?? 0) > 11
                    ? DateTime.tryParse(schedules?.matchTime ?? "")
                        ?.formatedString("yyyy/MM/dd\nHH:mm")
                    : (schedules?.matchTime ?? "").split(" ").join("\n"),
                fontSize: 12,
                color: Colours.N7),
            Expanded(
              flex: 2,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  QsText(schedules?.leftName ?? '',
                          fontSize: 14, color: Colours.N9, maxLines: 2)
                      .paddingOnly(left: 25),
                  const SizedBox(height: 8),
                  QsText(schedules?.rightName ?? '',
                          fontSize: 14, color: Colours.N9, maxLines: 2)
                      .paddingOnly(left: 25),
                ],
              ),
            ),
            Container(
                width: 20,
                alignment: Alignment.center,
                margin: const EdgeInsets.symmetric(horizontal: 8),
                child: DefaultTextStyle(
                    style: const TextStyle(fontSize: 15, color: Colours.N7),
                    child: scoreSchedule())),
            Container(
                width: 80,
                alignment: Alignment.centerRight,
                child: statusSchedule() != null
                    ? Container(
                        padding: const EdgeInsets.symmetric(
                            horizontal: 4, vertical: 2),
                        decoration: BoxDecoration(
                            color: Colours.B1,
                            borderRadius: BorderRadius.circular(2)),
                        child: statusSchedule())
                    : null)
          ],
        ),
      ),
    );
  }

  Widget? scoreMore() {
    if (schedules?.leftScoreOT != null && schedules?.leftScorePK == null) {
      return QsText(
        '加时 ${schedules?.leftScoreOT}-${schedules?.rightScoreOT}',
        fontSize: 10,
        color: Colours.B6,
      );
    } else if (schedules?.leftScoreOT == null &&
        schedules?.leftScorePK != null) {
      return QsText('点球 ${schedules?.leftScorePK}-${schedules?.rightScorePK}',
          fontSize: 10, color: Colours.B6);
    } else if (schedules?.leftScoreOT != null &&
        schedules?.leftScorePK != null) {
      return const QsText('加｜点', fontSize: 10, color: Colours.B6);
    } else {
      return null;
    }
  }

  Color scoreColor(bool flag) {
    int left = 0;
    int right = 0;
    if (schedules?.leftScoreEnd != null && schedules?.rightScoreEnd != null) {
      left = schedules!.leftScoreEnd! +
          (schedules?.leftScoreOT ?? 0) +
          (schedules?.leftScorePK ?? 0);
      right = schedules!.rightScoreEnd! +
          (schedules?.rightScoreOT ?? 0) +
          (schedules?.rightScorePK ?? 0);
    }
    if (left > right) {
      return flag ? Colours.N9 : Colours.N6;
    } else if (left < right) {
      return flag ? Colours.N6 : Colours.N9;
    } else {
      return Colours.N6;
    }
  }

  int scoreScheduleResult() {
    int left = (schedules?.leftScoreEnd ?? 0) +
        (schedules?.leftScoreOT ?? 0) +
        (schedules?.leftScorePK ?? 0);
    int right = (schedules?.rightScoreEnd ?? 0) +
        (schedules?.rightScoreOT ?? 0) +
        (schedules?.rightScorePK ?? 0);

    return (left > right)
        ? 1
        : (left < right)
            ? -1
            : 0;
  }

  Widget buildScoreRow(String score, Color color, bool isWinner) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        QsText(
          score,
          fontSize: 16,
          color: color,
          fontWeight: FontWeight.w800,
          fontFamily: 'D-DIN-PRO',
        ),
        const SizedBox(width: 2),
        if (isWinner)
          Image.asset(
            width: 6,
            Utils.getImgPath("data_left_arrow.png"),
          ),
      ],
    );
  }

  Widget scoreSchedule() {
    int scoreResult = scoreScheduleResult();
    bool isLeftWinner = scoreResult == 1;
    bool isRightWinner = scoreResult == -1;

    String displayLeftScore =
        schedules?.status == 7 ? '${schedules?.leftScoreEnd ?? '-'}' : '-';
    String displayRightScore =
        schedules?.status == 7 ? '${schedules?.rightScoreEnd ?? '-'}' : '-';

    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        buildScoreRow(
          displayLeftScore,
          isLeftWinner ? Colours.N9 : Colours.N7,
          isLeftWinner,
        ),
        const SizedBox(height: 10),
        buildScoreRow(
          displayRightScore,
          isRightWinner ? Colours.N9 : Colours.N7,
          isRightWinner,
        ),
      ],
    );
  }

  Widget? statusSchedule() {
    switch (schedules?.status) {
      case 7:
        if (schedules?.leftScoreEnd != null) {
          return scoreMore();
        } else {
          return null;
        }
      case 8:
        return const QsText("取消", fontSize: 10, color: Colours.B6);
      case 9:
        return const QsText("中断", fontSize: 10, color: Colours.B6);
      case 10:
        return const QsText("腰斩", fontSize: 10, color: Colours.B6);
      case 11:
        return const QsText("延期", fontSize: 10, color: Colours.B6);
      case 12:
        return const QsText("待定", fontSize: 10, color: Colours.B6);
      default:
        return null;
    }
  }

  Widget statusCheck() {
    if (recordType == 0) {
      switch (schedules?.status) {
        case 7:
          return Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              if (schedules?.leftScoreEnd != null)
                QsText(
                  '${schedules?.leftScoreEnd}-${schedules?.rightScoreEnd}',
                  fontSize: 15,
                  color: needColor ? scoreColor(true) : Colours.text,
                  fontWeight: FontWeight.w500,
                ),
              //scoreMore()
            ],
          );
        case 8:
          return const QsText("取消");
        case 9:
          return const QsText("中断");
        case 10:
          return const QsText("腰斩");
        case 11:
          return const QsText("延期");
        case 12:
          return const QsText("待定");
        default:
          return const QsText("vs");
      }
    } else {
      switch (schedules?.status) {
        case 10:
          return QsText(
            '${schedules?.leftScoreEnd}-${schedules?.rightScoreEnd}',
            fontSize: 15,
            color: needColor ? scoreColor(true) : Colours.text,
            fontWeight: FontWeight.w500,
          );
        case 11:
          return const QsText("取消");
        case 12:
          return const QsText("中断");
        case 13:
          return const QsText("腰斩");
        case 14:
          return const QsText("延期");
        case 15:
          return const QsText("待定");
        default:
          return const QsText("vs");
      }
    }
  }
}
